app.items = {
	init : function() {
		
	}
};

/**
 * Base item object
 */
app.item = {
	id : '',
	name : '',
	attributes : {},
	slot : null,
	area : false,
	
	/**
	 * Get item attribute
	 *
	 * @param name Attribute name
	 * @return Attribute value
	 */
	getAttr : function(name) {
		var value = 0;
		var attribute, value, max;
		
		attribute = this.attributes[name];
		
		if (attribute) {
			value = attribute.value;
			max = attribute.maxValue;
		
			if ('damage' == name) {
				if (max != value) {
					value += Math.round(Math.random() * (max - value));
				}
			}
		}
		
		return value;
	},
	
	/**
	 * Get attribute max value
	 *
	 * @param name Attribute name
	 * @return Attribute max value
	 */
	getAttrMax : function(name) {
		var value = 0;
		
		value = this.attributes[name].maxValue;
		
		return value;
	},
	
	/**
	 * Set attribute value
	 *
	 * @param name Attribute name
	 * @param value New attribute value
	 */
	setAttr : function(name, value) {
		this.attributes[name].value = value;
	},
	
	/**
	 * Modify attribute value
	 *
	 * @param name Attribute name
	 * @param value Attribute value
	 */
	modifyAttr : function(name, value) {
		value = this.getAttr(name) + (+value);
		
		this.setAttr(name, value);
	},
	
	/**
	 * Reduce item durability
	 */
	reduceDurability : function() {
		if (this.isBroken()) {
			return false;
		}
		
		this.setAttr('durability', this.getAttr('durability') - this.attributes['durability'].use);
	},
	
	/**
	 * Check if item is broken and cannot be used
	 *
	 * @return "true" if broken
	 */
	isBroken : function() {
		return this.getAttr('durability') <= 0;
	},
};

app.ItemsMgr = {
	_items : [],
	_itemsIndex : {},
	
	load : function(list) {
		var that = this;
		
		$.each(list, function(id, options) {
			var item = null;
			var base = '';
			var index = 0;
			
			if (options.attributes) {
				for (var attr in options.attributes) {
					if (!options.attributes[attr].maxValue) {
						options.attributes[attr].maxValue = options.attributes[attr].value;
					}
					
					if (!options.attributes[attr].ratio) {
						options.attributes[attr].ratio = 0;
					}
				}
			}
			
			options.id = options.id || id;
			
			if (options.base) {
				base = that.get(options.base);
				delete options.base;
			}
			
			item = $.extend(true, {}, base || app.item, options);
			index = that._items.push(item) - 1;
			
			that._itemsIndex[id] = index;
		});
	},
	
	create : function(type, options) {
		options = options || {};
		type = this.get(type);
		
		return $.extend(true, {}, type, options);
	},
	
	get : function(id) {
		return this._items[this._itemsIndex[id]];
	},
	
	getRandom : function() {
		return this._items[parseInt(Math.random() * this._items.length)];
	}
};

(function() {
	var items = {
		'weapon' : {
			name : 'Weapon',
			attributes : {
				'damage' : {value : 1},
				'durability' : {value : 100, use : 1}
			},
			slot : 'hand'
		},
		
		/**
		 * =========================
		 * ========= SWORDS =========
		 * =========================
		 */
		'blade' : {
			base : 'weapon',
			name : 'Blade',
			attributes : {
				'durability' : {value : 70}
			}
		},
		'knife' : {
			base : 'blade',
			name : 'Knife'
		},
		'sword' : {
			base : 'blade',
			name : 'Sword',
			attributes : {
				damage : {value : 1, maxValue : 2},
				durability : {value : 100}
			}
		},
		
		/**
		 * =====================
		 * ======= RANGED =======
		 * =====================
		 */
		'bow' : {
			base : 'weapon',
			name : 'Bow',
			attributes : {
				damage : {value : 1, maxValue : 4},
				durability : {value : 150}
			}
		},
		
		/**
		 * =====================
		 * ====== ACCESSORY ======
		 * =====================
		 */
		'accessory' : {
			name : 'Accessory'
		},
		'ring' : {
			name : 'Ring',
			slot : 'ring',
			attributes : {}
		},
		
		'admin-ring' : {
			name : 'Admin ring',
			base : 'ring',
			attributes : {
				health : {value : 250},
				damage : {value : 100},
				strength : {value : 100},
				agility : {value : 100}
			}
		},
		
		'amulet' : {
			name : 'Amulet',
			slot : 'amulet',
			attributes : {}
		}
	};
	
	app.ItemsMgr.load(items);
})();